package com.example.web.office_specifications.controller;


import com.example.utils.ResultUtils;
import com.example.utils.ResultVo;
import com.example.web.office_specifications.entity.MakeSpecificationsTree;
import com.example.web.office_specifications.entity.OfficeSpecifications;
import com.example.web.office_specifications.service.IOfficeSpecificationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LS
 * @since 2024-05-16
 */
@Api(tags = "规格书管理接口")
@RestController
@RequestMapping("api/office-specifications")
public class OfficeSpecificationsController {

    private static final String OfficeSpecifications_LIST = "OfficeSpecifications_LIST";
    @Resource
    private RedisTemplate<String, OfficeSpecifications> redisTemplate;
    @Resource
    private IOfficeSpecificationsService officeSpecificationsService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public ResultVo getList() {

        List<OfficeSpecifications> list = redisTemplate.opsForList().range(OfficeSpecifications_LIST, 0, -1);
        if (list == null || list.isEmpty()) {
            //取出来数据是空的
            list = officeSpecificationsService.list();
            if (!list.isEmpty())
                redisTemplate.opsForList().leftPushAll(OfficeSpecifications_LIST, list);
        }
        //排序
        list = list.stream().sorted(Comparator.comparing(OfficeSpecifications::getId)).collect(Collectors.toList());
        //组装树数据
        List<OfficeSpecifications> menuList = MakeSpecificationsTree.makeTree(list, 0L);
        return ResultUtils.success("查询成功", menuList);
    }

    @ApiOperation("新增")
    @PostMapping
    public ResultVo add(@RequestBody OfficeSpecifications officeSpecifications) {
        if (officeSpecificationsService.save(officeSpecifications)) {
            //将新增的数据加入缓存
            redisTemplate.opsForList().leftPush(OfficeSpecifications_LIST, officeSpecifications);
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    @ApiOperation("编辑")
    @PutMapping
    public ResultVo edit(@RequestBody OfficeSpecifications officeSpecifications) {
        OfficeSpecifications specifications = officeSpecificationsService.getById(officeSpecifications.getId());
        if (officeSpecificationsService.updateById(officeSpecifications)) {
            //先删除原来的缓存
            redisTemplate.opsForList().remove(OfficeSpecifications_LIST, 0, specifications);
            //将更新的数据加入缓存
            redisTemplate.opsForList().leftPush(OfficeSpecifications_LIST, officeSpecifications);
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    public ResultVo delete(@PathVariable Long id) {
        if (officeSpecificationsService.deleteNode(id)) {
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

}

